package com.wisnews.controller.sr;


import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.wisnews.pojo.Result;
import com.wisnews.pojo.sr.entity.Member;
import com.wisnews.service.Impl.RedisLikeServiceImpl;
import com.wisnews.util.Constants;
import com.wisnews.util.MemberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yuyd
 * @since 2021-03-21
 */
@RestController
@RequestMapping("/post-like")
public class PostLikeController {

    @Autowired
    private RedisLikeServiceImpl redisLikeService;

    @PostMapping("like/{postId}")
    public Result likePost(@PathVariable(value = "postId",required = true) String postId, HttpServletRequest request){

        Member currentUser = MemberUtils.getLoginUserInfo(request);

        if (ObjectUtils.isNull(currentUser)){
            return new Result(Constants.SYS_CODE.FAIL, Constants.SYS_CODE.FAIL_MESSAGE,"点赞失败，请登录后重试！");
        }

        redisLikeService.saveLiked2Redis(currentUser.getUserId(),postId);

        return new Result(Constants.SYS_CODE.SUCCESS, Constants.SYS_CODE.SUCCESS_MESSAGE);
    }



    //点赞
    @PostMapping("unlike/{postId}")
    public Result unlikePost(@PathVariable(value = "postId",required = true) String postId, HttpServletRequest request){

        Member currentUser = MemberUtils.getLoginUserInfo(request);

        redisLikeService.unlikeFromRedis(currentUser.getUserId(),postId);

        return new Result(Constants.SYS_CODE.SUCCESS, Constants.SYS_CODE.SUCCESS_MESSAGE);
    }

}
